﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using System.IO.Ports;
using System.Timers;
using System.Globalization;

namespace DataRecv.tools
{
    /// <summary>
    /// 串口硬看门狗
    /// </summary>
    public class WatchDog
    {
        public delegate void ShutDown();
        public event ShutDown ShutDownEvent;

        public SerialPort serialport = null;
        private Timer timer = null;
        private static byte[] buffer_feeddog = new byte[] {
                                                    byte.Parse("EF",NumberStyles.HexNumber),
                                                    byte.Parse("C5",NumberStyles.HexNumber),
                                                    byte.Parse("00",NumberStyles.HexNumber),
                                                    byte.Parse("00",NumberStyles.HexNumber),
                                                    byte.Parse("C5",NumberStyles.HexNumber),
                                                    byte.Parse("FE",NumberStyles.HexNumber)
                                               };
        private static byte[] buffer_enabledog = new byte[] {
                                                    byte.Parse("EF",NumberStyles.HexNumber),
                                                    byte.Parse("C5",NumberStyles.HexNumber),
                                                    byte.Parse("00",NumberStyles.HexNumber),
                                                    byte.Parse("00",NumberStyles.HexNumber),
                                                    byte.Parse("C5",NumberStyles.HexNumber),
                                                    byte.Parse("FE",NumberStyles.HexNumber)
                                               };
        private static byte[] buffer_disabledog = new byte[] {
                                                    byte.Parse("EF",NumberStyles.HexNumber),
                                                    byte.Parse("C6",NumberStyles.HexNumber),
                                                    byte.Parse("00",NumberStyles.HexNumber),
                                                    byte.Parse("00",NumberStyles.HexNumber),
                                                    byte.Parse("C6",NumberStyles.HexNumber),
                                                    byte.Parse("FE",NumberStyles.HexNumber)
                                               };
        public static byte[] MakeShutDownBytes(string shutDownMin)
        {
            byte[] ans = new byte[6];
            ans[0] = byte.Parse("EF", NumberStyles.HexNumber);
            ans[1] = byte.Parse("C7", NumberStyles.HexNumber);
            ans[2] = byte.Parse("00", NumberStyles.HexNumber);
            ans[3] = byte.Parse(shutDownMin, NumberStyles.HexNumber);
            ans[4] = (byte)(ans[1] + ans[2] + ans[3]);
            ans[5] = byte.Parse("FE", NumberStyles.HexNumber);
            return ans;
        }
        //使用看门狗lSendStr := Chr($EF) + Chr($C5) + Chr($00) + Chr($00) + Chr($C5) + Chr($FE);
        //停用看门狗lSendStr := Chr($EF) + Chr($C6) + Chr($00) + Chr($00) + Chr($C6) + Chr($FE);
        //喂狗5秒一次lSendStr := Chr($EF) + Chr($C5) + Chr($00) + Chr($00) + Chr($C5) + Chr($FE);
        //断电关机
        //  for lIndex := 1 to 3 do
        //begin
        //  lSendStr := Chr($EF) + Chr($C7) + Chr(gPowerOff_H) + Chr(gPowerOff_M)
        //    + Chr($C7 + gPowerOff_H + gPowerOff_M) + Chr($FE);
        //  try
        //    WriteCom('重启动命令', lSendStr);
        //  except on E: exception do
        //    gLog_Fault.Add('Timer_ResetTimer---' + E.Message);
        //  end;
        //  Sleep(500);
        //end;
        public string m_ComPort;
        public bool m_EnableShutDown;
        public string m_ShutDownHour;
        public string m_ShutDownMin;
        public string m_ShutDownMinSpan;
        /// <summary>
        /// 构造函数
        /// </summary>
        public WatchDog(string comPort, bool enableShutDown, string shutDownHour, string shutDownMin, string shutDownMinSpan)
        {
            m_ComPort = comPort;
            m_EnableShutDown = enableShutDown;
            m_ShutDownHour = shutDownHour;
            m_ShutDownMin = shutDownMin;
            m_ShutDownMinSpan = shutDownMinSpan;
            serialport = new SerialPort(comPort, 9600, Parity.None, 8, StopBits.One);
            timer = new Timer();
            timer.Interval = 5000;
            timer.Elapsed += new ElapsedEventHandler(t_Elapsed);
            timer.Start();
        }

        /// <summary>
        /// 喂狗
        /// </summary>
        private void Feed()
        {
            try
            {
                if (serialport != null)
                {
                    if (!serialport.IsOpen)
                    {
                        serialport.Open();
                    }
                    if (serialport.IsOpen)
                    {
                        serialport.Write(buffer_feeddog, 0, 6);
                    }
                }
            }
            catch
            {

            }
        }

        /// <summary>
        /// 使用看门狗
        /// </summary>
        public void EnableWatchDog()
        {
            try
            {
                if (serialport != null)
                {
                    if (!serialport.IsOpen)
                    {
                        serialport.Open();
                    }
                    if (serialport.IsOpen)
                    {
                        serialport.Write(buffer_enabledog, 0, 6);
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// 停用看门狗
        /// </summary>
        public void DisableWatchDog()
        {
            try
            {
                if (serialport != null)
                {
                    if (!serialport.IsOpen)
                    {
                        serialport.Open();
                    }
                    if (serialport.IsOpen)
                    {
                        serialport.Write(buffer_disabledog, 0, 6);
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// 断电关机
        /// </summary>
        public void WatchDogShutDown(string min)
        {
            try
            {
                if (serialport != null)
                {
                    if (!serialport.IsOpen)
                    {
                        serialport.Open();
                    }
                    if (serialport.IsOpen)
                    {
                        serialport.Write(MakeShutDownBytes(min), 0, 6);
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// 定时器回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void t_Elapsed(object sender, ElapsedEventArgs e)
        {
            Feed();
            if (m_EnableShutDown)
            {
                DateTime time = DateTime.Now;
                if ((time.Hour == int.Parse(m_ShutDownHour)) && (time.Minute == int.Parse(m_ShutDownMin)))
                {
                    for (int i = 0; i < 3; i++)
                    {
                        WatchDogShutDown(m_ShutDownMinSpan);
                    }
                    if (ShutDownEvent != null) ShutDownEvent();
                }
            }
        }

        /// <summary>
        /// 回收资源
        /// </summary>
        public void Dispose()
        {
            try
            {
                if (timer != null) timer.Stop();
                if (timer != null) timer.Dispose();
                if (serialport != null)
                {
                    serialport.Close();
                    serialport.Dispose();
                }
            }
            catch
            { }
        }
    }
}
